package com.boarsoft.boar.message.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.boarsoft.bean.ReplyInfo;
import com.boarsoft.boar.common.Constants;
import com.boarsoft.boar.message.flow.MessageStr;
import com.boarsoft.boar.message.service.MsgServerService;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.flow.core.SimpleFlow;
import com.boarsoft.flow.core.SimpleFlowEngine;
import com.boarsoft.message.bean.MessageEntity;
import com.boarsoft.message.bean.TargetQueueInfo;

@RestController
@RequestMapping("/broker")
public class MessageBrokerAction {
	private static final Logger log = LoggerFactory.getLogger(MessageBrokerAction.class);

	@Autowired
	private MsgServerService msgBrokerSvc;
	@Autowired
	private SimpleFlowEngine simpleFlowEngine;

	/**
	 * 从RPC获取中间件列表(boarmq)
	 */
	@RequestMapping("/list.do")
	public ReplyInfo<Object> list() {
		//TODO 当前只有从boarmq获取broker，需改成支持全部broker地址的获取
		return new ReplyInfo<Object>(true, msgBrokerSvc.list());
	}

	@RequestMapping("/queue.do")
	public ReplyInfo<Object> queue(String addr) {
		//TODO 当前只有从boarmq获取queue，需改成支持全部broker地址的获取
		if (Util.strIsEmpty(addr)) {
			return new ReplyInfo<Object>(Constants.WARN_INVALID, MessageStr.ADDR);
		}
		List<TargetQueueInfo> queues = msgBrokerSvc.getQueueInfo(addr);
		PagedResult<TargetQueueInfo> pr = new PagedResult<TargetQueueInfo>(queues.size(), queues, 1, 1000);
		return new ReplyInfo<Object>(true, pr);
	}

	@RequestMapping("/message.do")
	public ReplyInfo<Object> message(String target, String addr, String messageId, Date startTime, Date endTime, String key,
			short status, int pageNo, int pageSize) {
		//TODO 当前只有从boarmq获取message，需改成支持全部broker地址的获取
		if (Util.strIsEmpty(target)) {
			return new ReplyInfo<Object>(Constants.WARN_INVALID, MessageStr.TARGET);
		}
		if (Util.strIsEmpty(addr)) {
			return new ReplyInfo<Object>(Constants.WARN_INVALID, MessageStr.ADDR);
		}
		if (pageSize < 1) {
			return new ReplyInfo<Object>(Constants.WARN_INVALID, MessageStr.PAGE_SIZE);
		}
		List<MessageEntity> list = msgBrokerSvc.list(//
				target, addr, messageId, startTime, endTime, status, key, pageNo, pageSize);
		if (list == null) {
			return new ReplyInfo<Object>(Constants.WARN_INVALID, MessageStr.TARGET);
		}
		PagedResult<MessageEntity> pr = new PagedResult<>(list.size(), list);
		return new ReplyInfo<Object>(true, pr);
	}

	@RequestMapping("/msg.do")
	@SuppressWarnings("unchecked")
	public ReplyInfo<Object> msg(String target, String addr, String messageId, String startTime, String endTime, String key,
			short status) {
		Map<String, Object> req = new HashMap<>();
		req.put(MessageStr.TARGET, target);
		req.put(MessageStr.ADDR, addr);
		req.put(MessageStr.KEY, key);
		req.put(MessageStr.MESSAGE_ID, messageId);
		req.put(MessageStr.START_TIME, startTime);
		req.put(MessageStr.END_TIME, endTime);
		req.put(MessageStr.STATUS, status);
		try {
			if (simpleFlowEngine.exists(MessageStr.FLOW_ID)) {
				SimpleFlow f = simpleFlowEngine.start(MessageStr.FLOW_ID, req);
				Map<String, Object> map = (Map<String, Object>) f.getData();
				return new ReplyInfo<Object>(true, map.get(MessageStr.MSG_LIST));
			} else {
				log.info("没找到流程配置文件：{}", MessageStr.FLOW_ID);
				return new ReplyInfo<Object>(Constants.WARN_INVALID, "没找到流程配置文件");
			}
		} catch (Throwable e) {
			log.error("query each addr queue message error: ", e);
			return new ReplyInfo<Object>(Constants.WARN_INVALID, e.toString());
		}
	}

	@RequestMapping("/count.do")
	public ReplyInfo<Object> count(String target, Date startTime, Date endTime) throws Throwable {
		if (Util.strIsEmpty(target)) {
			return new ReplyInfo<Object>(Constants.WARN_INVALID, MessageStr.TARGET);
		}
		List<Map<String, Object>> list = msgBrokerSvc.count(target, startTime, endTime);
		if (list == null) {
			return new ReplyInfo<Object>(Constants.ERROR_UNKNOWN);
		}
		Map<String, List<Map<String, Object>>> result = new HashMap<>();
		result.put("list", list);
		return new ReplyInfo<Object>(true, result);
	}
}